<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录 - 名片管理系统</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body {
      background-color: #f8f9fa;
    }
    .login-container {
      max-width: 400px;
      margin: 100px auto;
      padding: 20px;
      background-color: white;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    .error-message {
      color: #dc3545;
      margin-top: 10px;
      display: none;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="login-container">
      <h2 class="text-center mb-4">登录</h2>
      <form id="loginForm">
        <div class="mb-3">
          <label for="username" class="form-label">用户名</label>
          <input type="text" class="form-control" id="username" required>
        </div>
        <div class="mb-3">
          <label for="password" class="form-label">密码</label>
          <input type="password" class="form-control" id="password" required>
        </div>
        <div class="d-grid gap-2">
          <button type="submit" class="btn btn-primary">登录</button>
        </div>
        <div class="text-center mt-3">
          <a href="register.html">注册新账号</a> | 
          <a href="forgot-password.html">忘记密码？</a>
        </div>
        <div id="errorMessage" class="error-message text-center"></div>
      </form>
    </div>
  </div>

  <script src="js/api.js"></script>
  <script>
    document.getElementById('loginForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      
      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const errorMessage = document.getElementById('errorMessage');
      
      // 隐藏之前显示的错误消息
      errorMessage.style.display = 'none';
      
      // 禁用登录按钮，防止重复提交
      const submitButton = document.querySelector('button[type="submit"]');
      submitButton.disabled = true;
      submitButton.innerHTML = '登录中...';
      
      try {
        const response = await api.auth.login({ username, password });
        console.log('登录响应:', response); // 调试用
        
        // 检查响应格式
        if (!response.success) {
          throw new Error(response.message || '登录失败');
        }
        
        if (!response.token) {
          throw new Error('登录失败：服务器响应缺少令牌');
        }
        
        // 存储用户信息
        localStorage.setItem('currentUser', JSON.stringify({
          id: response.userId, // 后端返回的是userId，不是id
          username: response.username
        }));
        
        // 单独存储令牌
        localStorage.setItem('token', response.token);
        
        // 跳转到主页
        window.location.href = 'main.html';
      } catch (error) {
        console.error('登录失败:', error);
        errorMessage.textContent = error.message || '登录失败，请检查用户名和密码';
        errorMessage.style.display = 'block';
        
        // 重新启用登录按钮
        submitButton.disabled = false;
        submitButton.innerHTML = '登录';
      }
    });
  </script>
</body>

</html>